﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using NetJsWire.Widgets.Config;
using Newtonsoft.Json;
//using NetJsWire.Design.Converters;
//using NetJsWire.Design.Editors;

namespace NetJsWire.Widgets.Core
{
    [Serializable]
    [JsonObject( "listeners" )]
    //[TypeConverter( typeof( ListenerTypeConverter ) )]
    public class Listener
    {
        private EventOptions _options;

        public Listener()
        {
            _options = new EventOptions();
        }

        [DefaultValue( ( string ) null )]
        [Description( "The name of the event to listen for." )]
        [JsonProperty( "eventName" )]
        //[TypeConverter( typeof( ListenerEventNameTypeConverter ) )]
        public string EventName { get; set; }

        [DefaultValue( ( string ) null )]
        [Description( "The method the event invokes." )]
        [JsonProperty( "handler" )]
        //[Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        public string Handler { get; set; }

        [DefaultValue( ( string ) null )]
        [Description( "(optional) The scope (this reference) in which the handler function is executed. If omitted, defaults to the object which fired the event." )]
        [JsonProperty( "scope" )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        public string Scope { get; set; }

        [Description( "(optional) An object containing handler configuration. properties." )]
        [JsonProperty( "options", DefaultValueHandling = DefaultValueHandling.Ignore )]
        [TypeConverter( typeof( ExpandableObjectConverter ) )]
        public EventOptions Options
        {
            get { return _options; }
            set { _options = value; }
        }

        public bool ShouldSerializeOptions()
        {
            return !string.IsNullOrWhiteSpace( EventName ) && ( !string.IsNullOrWhiteSpace( Options.Scope ) || !string.IsNullOrWhiteSpace( Options.Target ) || Options.Buffer != 0 || Options.Delay != 0 || Options.Single );
        }

        protected void ResetOptions()
        {
            _options = new EventOptions();
        }

        public override string ToString()
        {
            return !string.IsNullOrWhiteSpace( EventName ) ? EventName : GetType().Name;
        }
    }
}
